package cn.edu.tju.fctl.rest;

import cn.edu.tju.fctl.entities.*;
import cn.edu.tju.fctl.rest.annotation.LeaveLogDetail;
import cn.edu.tju.fctl.service.IUserService;
import cn.edu.tju.fctl.service.IVacationService;
import cn.edu.tju.fctl.util.*;
import cn.edu.tju.fctl.util.response.ResponseType;
import org.codehaus.jackson.JsonEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Created by liutong on 15/12/18.
 */
@Component
@Path("vacation")
public class VacationRest {
    @Autowired
    IVacationService vacationService;
    @Autowired
    IUserService userService;

    @LeaveLogDetail(value = "提交了申请", hasId = false)
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Object askForVacation(LeaveInfo leaveInfo) {
        Object response = new Object();
        leaveInfo.setUserId(ShiroUtil.getUser().getUserId());
        leaveInfo.setStatus(VacationStatus.WAITING_FOR_FIRST_APPLY.toString());
        vacationService.createLeave(leaveInfo);
        int leaveId = leaveInfo.getLeaveId();
        response = StateCode.buildCode(BizCode.SUCCESS);
        ((StateCode) response).addData("leaveId", leaveId);

        EmailUtil.sendOneMail(ShiroUtil.getUser().getEmail(), "提交了申请假期", leaveInfo.getReason());
        return response;
    }

    @GET
    @Path("/test")
    public void testEmail(){
        EmailUtil.sendOneMail("catoldcui@gmail.com", "提交了申请假期", "test");

    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getVacations() {
        Object response = new Object();
        User user = ShiroUtil.getUser();
        response = vacationService.findAllVacationByUserID(user.getUserId());
        return response;
    }

    @Path("{id:[0-9]*}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getVacationDetail(@PathParam("id") final int id) {
        Object response = new Object();
        ResponseType responseType = new ResponseType();
//        responseType.setUser(ShiroUtil.getUser());
        responseType.setVacationInfo(vacationService.findById(id));
        responseType.setVacationLogs(vacationService.findVacationLogByID(id));
        response = responseType;
        return response;
    }

    @Path("{id:[0-9]*}/delay/{delay_id:[0-9]*}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getVacationDelayDetail(@PathParam("id") final int id,
                                         @PathParam("delay_id") final int delay_id) {
        Object response = new Object();
        response = vacationService.findDelayVacation(delay_id);
        return response;
    }

    @LeaveLogDetail(value = "提交了延期申请", hasId = false)
    @Path("{id:[0-9]*}/delay/")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Object askForDelayVacation(@PathParam("id") final int id,
                                      final LeaveDelay leaveDelay) {
        Object response = new Object();
        leaveDelay.setLeaveId(id);
        leaveDelay.setStatus(VacationStatus.WAITING_FOR_FIRST_APPLY.toString());
        vacationService.addVacationToDelay(leaveDelay);
        response = StateCode.buildCode(BizCode.SUCCESS);
        ((StateCode) response).addData("delayId", leaveDelay.getLeaveDelayId());
        EmailUtil.sendOneMail(ShiroUtil.getUser().getEmail(), "提交了延期申请", leaveDelay.getReason());

        return response;
    }

    @LeaveLogDetail(value = "更新了申请的状态")
    @Path("{id:[0-9]*}/status/{status}")
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    public Object changeVacationStatus(@PathParam("id") final int id,
                                       @PathParam("status") final String status) {
        Object response = new Object();
        LeaveInfo leaveInfo = vacationService.findById(id);
        leaveInfo.setStatus(status);
        vacationService.updateLeave(leaveInfo);
        response = StateCode.buildCode(BizCode.SUCCESS);

        EmailUtil.sendOneMail(userService.findById(leaveInfo.getUserId()).getEmail(), "更新假期申请的状态", ShiroUtil.getUser().getName() + "将申请状态变成为" + VacationStatus.valueOf(status).getName());

        return response;
    }

    @LeaveLogDetail(value = "修改了延期申请状态")
    @Path("{id:[0-9]*}/delay/{delay_id:[0-9]*}/status/{status}")
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    public Object changeDelayVacationStatus(@PathParam("id") final int id,
                                            @PathParam("delay_id") final int delay_id,
                                            @PathParam("status") final String status) {
        Object response = new Object();
        LeaveDelay leaveDelay = vacationService.findDelayVacation(delay_id);
        leaveDelay.setStatus(status);
        vacationService.updateLeaveDelay(leaveDelay);
        LeaveInfo leaveInfo = vacationService.findById(id);

        // 操作延期申请时,更新一级状态  延期一级通过 延期二级痛过
        if (VacationStatus.SECOND_PASSED.toString().equals(status)) {
            leaveInfo.setStatus(VacationStatus.DELAY_PASSED.toString());
            vacationService.updateLeave(leaveInfo);
        } else if(VacationStatus.SECOND_BLOCKED.toString().equals(status)){
            leaveInfo.setStatus(VacationStatus.DELAY_BLOCKED.toString());
            vacationService.updateLeave(leaveInfo);
        }
        EmailUtil.sendOneMail(userService.findById(leaveInfo.getUserId()).getEmail(), "更新假期延期申请的状态", ShiroUtil.getUser().getName() + "将申请状态变成为" + VacationStatus.valueOf(status).getName());

        response = StateCode.buildCode(BizCode.SUCCESS);
        return response;
    }
}
